/**
 * 
 */
package com.brucexx.wedding.dao;

import java.util.Collection;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author BruceXX email:brucest0078@gmail.com
 * 
 */
public class BaseModelDAO {

	private static final Log logger = LogFactory.getLog(BaseModelDAO.class);

	private static final PersistenceManagerFactory pmFactory = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	@SuppressWarnings("unchecked")
	public static <T> List<T> executeSqlForList(String gql) {
		PersistenceManager pm = getPesistentManager();
		try {
			List<T> list = (List<T>) pm.newQuery(gql).execute();
			logger.info("query list sql[" + gql + "]resultSize==>"
					+ list.size());
			return list;
		} finally {
			pm.close();
		}
	}

	public static <T> List<T> executeSqlForPage(String gql, long start, long end) {
		PersistenceManager pm = getPesistentManager();
		try {
			Query query = pm.newQuery(gql);
			query.setRange(start, end);
			List<T> list = (List<T>) query.execute();
			logger.info("query page sql[" + gql + "],start=" + start + ",end="
					+ end + ",resultSize==>" + list.size());
			return list;
		} finally {
			pm.close();
		}
	}

	public static <T> int getCountBySql(String gql) {
		PersistenceManager pm = getPesistentManager();
		try {
			Query query = pm.newQuery(gql);
			query.setResult("count(this)");
			return (Integer) query.execute();
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public static <T> List<T> loadAll(Class<T> clz) {
		PersistenceManager pm = getPesistentManager();
		try {
			List<T> list = (List<T>) pm
					.newQuery("select from " + clz.getName()).execute();
			logger.info("query all Object[" + clz.getName() + "]resultSize==>"
					+ list.size());
			return list;
		} finally {
			pm.close();
		}

	}

	@SuppressWarnings("unchecked")
	public static <T> T executeSqlForModel(String gql) {
		PersistenceManager pm = getPesistentManager();
		try {
			logger.info("query sql[" + gql + "]");
			return (T) pm.newQuery(gql).execute();
		} finally {
			pm.close();
		}
	}

	public static <T> T loadById(long id, Class<T> clz) {
		PersistenceManager pm = getPesistentManager();
		try {
			return pm.getObjectById(clz, id);
		} catch (Exception e) {
			return null;
		} finally {
			pm.close();
		}
	}

	public static <T> void addOrUpdate(T obj) {
		PersistenceManager pm = getPesistentManager();
		try {
			logger.info("update obj[" + obj + "]");
			pm.makePersistent(obj);
		} finally {
			pm.close();
		}
	}

	public static <T> T persistAndGet(T obj) {
		PersistenceManager pm = getPesistentManager();
		try {
			logger.info("update obj[" + obj + "]");
			return pm.makePersistent(obj);
		} finally {
			pm.close();
		}
	}

	public static <T> void deleteByID(Class<T> clz, long id) {
		PersistenceManager pm = getPesistentManager();
		try {
			T obj = pm.getObjectById(clz, id);
			logger.info("delete obj[" + obj + "]");
			pm.deletePersistent(obj);
		} finally {
			pm.close();
		}

	}

	public static <T> void deleteList(Collection<T> objs) {
		PersistenceManager pm = getPesistentManager();
		try {
			pm.deletePersistentAll(objs);
		} finally {
			pm.close();
		}
	}

	public static void execute(String gql) {
		PersistenceManager pm = getPesistentManager();
		try {
			logger.info("execute gql[" + gql + "]");
			pm.newQuery(gql).execute();
		} finally {
			pm.close();
		}

	}

	private static PersistenceManager getPesistentManager() {
		return pmFactory.getPersistenceManager();
	}

}
